// esbuild.js

const esbuild = require('esbuild');

const extensionConfig = {
  entryPoints: ['src/extension.ts'],
  bundle: true,
  outfile: 'dist/extension.js',
  external: ['vscode'],
  format: 'cjs',
  platform: 'node',
};

const webviewConfig = {
  entryPoints: ['src/webview-ui/index.tsx'],
  bundle: true,
  outfile: 'dist/webview.js',
  format: 'esm', // 重要：匹配 index.html 中的 type="module"
  platform: 'browser', // 重要：目标是浏览器环境
  loader: { // 告诉 esbuild如何处理各种文件
    '.tsx': 'tsx',
    '.ts': 'ts',
    '.svg': 'text', // 如果你在组件中导入了SVG
  },
};

async function build() {
  try {
    await esbuild.build(extensionConfig);
    await esbuild.build(webviewConfig);
    console.log('Build successful!');
  } catch (e) {
    console.error('Build failed:', e);
    process.exit(1);
  }
}

if (process.argv.includes('--watch')) {
  esbuild.context(extensionConfig).then(ctx => ctx.watch());
  esbuild.context(webviewConfig).then(ctx => ctx.watch());
  console.log('Watching for changes...');
} else {
  build();
}

